/**
 * 收藏模块类型定义
 * 基于后端API文档：我的模块API.md
 */

// ==================== 基础类型 ====================

/**
 * 收藏项类型
 */
export type FavoriteItemType = 'book' | 'book_set' | 'audio'

/**
 * 收藏绘本项
 */
export interface FavoriteBookItem {
  id: number
  title: string
  cover: string
  readStatus: boolean
  favoriteTime: string
}

/**
 * 收藏绘本集项
 */
export interface FavoriteBookSetItem {
  id: number
  title: string
  cover: string
  bookCount: number
  favoriteTime: string
}

/**
 * 收藏音频项
 */
export interface FavoriteAudioItem {
  id: number
  title: string
  cover: string
  duration: string
  favoriteTime: string
}

/**
 * 通用收藏项
 */
export interface FavoriteItem {
  id: number
  itemType: FavoriteItemType
  itemId: number
  title: string
  cover: string
  description?: string
  language?: string
  favoriteTime: string
  readStatus?: boolean // 仅绘本
  bookCount?: number // 仅绘本集
  duration?: number // 仅音频
  favoriteType: FavoriteItemType
  remark?: string
}

// ==================== API响应类型 ====================

/**
 * 分页响应基础类型
 */
export interface PageResponse<T> {
  total: number
  list: T[]
}

/**
 * 收藏绘本列表响应
 */
export interface FavoriteBooksRespVO extends PageResponse<FavoriteItem> {}

/**
 * 收藏绘本集列表响应
 */
export interface FavoriteBookSetsRespVO extends PageResponse<FavoriteItem> {}

/**
 * 收藏音频列表响应
 */
export interface FavoriteAudiosRespVO extends PageResponse<FavoriteItem> {}

// ==================== 请求类型 ====================

/**
 * 分页请求参数
 */
export interface PageParams {
  page: number
  size: number
}

/**
 * 获取收藏列表请求参数
 */
export interface GetFavoritesParams extends PageParams {
  type?: FavoriteItemType
}

/**
 * 批量删除收藏请求
 */
export interface BatchDeleteFavoritesReqVO {
  items: Array<{
    targetType: FavoriteItemType
    targetId: number
  }>
}

// ==================== API响应包装类型 ====================

/**
 * 通用API响应格式
 */
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

/**
 * 获取收藏绘本列表API响应
 */
export type GetFavoriteBooksResponse = ApiResponse<FavoriteBooksRespVO>

/**
 * 获取收藏绘本集列表API响应
 */
export type GetFavoriteBookSetsResponse = ApiResponse<FavoriteBookSetsRespVO>

/**
 * 获取收藏音频列表API响应
 */
export type GetFavoriteAudiosResponse = ApiResponse<FavoriteAudiosRespVO>

/**
 * 删除收藏API响应
 */
export type DeleteFavoriteResponse = ApiResponse<void>

/**
 * 批量删除收藏API响应
 */
export type BatchDeleteFavoritesResponse = ApiResponse<void>

// ==================== 错误码类型 ====================

/**
 * 收藏模块错误码
 */
export enum FavoritesErrorCode {
  FAVORITE_NOT_FOUND = 50005,
}

/**
 * 错误信息映射
 */
export const FavoritesErrorMessages: Record<FavoritesErrorCode, string> = {
  [FavoritesErrorCode.FAVORITE_NOT_FOUND]: '收藏项不存在',
}

// ==================== 组件Props类型 ====================

/**
 * 收藏列表组件Props
 */
export interface FavoriteListProps {
  type: FavoriteItemType
  items: FavoriteItem[]
  loading?: boolean
  hasMore?: boolean
  onLoadMore?: () => void
  onDelete?: (item: FavoriteItem) => void
  onItemClick?: (item: FavoriteItem) => void
}

/**
 * 收藏项组件Props
 */
export interface FavoriteItemProps {
  item: FavoriteItem
  onDelete?: (item: FavoriteItem) => void
  onClick?: (item: FavoriteItem) => void
}

/**
 * 收藏分类标签Props
 */
export interface FavoriteCategoryTabsProps {
  activeType: FavoriteItemType | 'all'
  onTypeChange: (type: FavoriteItemType | 'all') => void
  counts?: Record<FavoriteItemType, number>
}

// ==================== 状态管理类型 ====================

/**
 * 收藏模块状态
 */
export interface FavoritesState {
  books: FavoriteBookItem[]
  bookSets: FavoriteBookSetItem[]
  audios: FavoriteAudioItem[]
  loading: boolean
  hasMore: Record<FavoriteItemType, boolean>
  total: Record<FavoriteItemType, number>
  error: string | null
}

/**
 * 收藏模块Actions
 */
export interface FavoritesActions {
  getFavoriteBooks: (params: PageParams) => Promise<void>
  getFavoriteBookSets: (params: PageParams) => Promise<void>
  getFavoriteAudios: (params: PageParams) => Promise<void>
  deleteFavorite: (type: FavoriteItemType, itemId: number) => Promise<void>
  batchDeleteFavorites: (items: BatchDeleteFavoritesReqVO['items']) => Promise<void>
  clearError: () => void
  resetList: (type: FavoriteItemType) => void
}

// ==================== 工具类型 ====================

/**
 * 收藏类型配置
 */
export interface FavoriteTypeConfig {
  key: FavoriteItemType | 'all'
  label: string
  icon: string
  color: string
}

/**
 * 默认收藏类型配置
 */
export const DEFAULT_FAVORITE_TYPES: FavoriteTypeConfig[] = [
  {
    key: 'all',
    label: '全部',
    icon: 'list',
    color: '#666666',
  },
  {
    key: 'book',
    label: '绘本',
    icon: 'book',
    color: '#ff6b6b',
  },
  {
    key: 'book_set',
    label: '绘本集',
    icon: 'folder',
    color: '#4ecdc4',
  },
  {
    key: 'audio',
    label: '音频',
    icon: 'sound',
    color: '#45b7d1',
  },
]

/**
 * 分页默认配置
 */
export const DEFAULT_PAGE_CONFIG = {
  page: 1,
  size: 20,
}

/**
 * 收藏时间格式化选项
 */
export interface FavoriteTimeFormatOptions {
  showTime?: boolean
  relative?: boolean
  format?: string
}

/**
 * 默认时间格式化选项
 */
export const DEFAULT_TIME_FORMAT: FavoriteTimeFormatOptions = {
  showTime: false,
  relative: true,
  format: 'YYYY-MM-DD',
}
